package http

import (
	"bytes"
	jsonTool "encoding/json"
	"fisherman/common/logger"
	"fmt"
	"io"
	"net/http"
	urlTool "net/url"
	"strings"
)

func Get(url string) string {
	return GetPH(url, nil, nil)
}

func GetP(url string, params map[string]any) string {
	return GetPH(url, params, nil)
}

func GetH(url string, header map[string]string) string {
	return GetPH(url, nil, header)
}

func GetPH(url string, params map[string]any, header map[string]string) string {
	if params != nil {
		paramsStr := UrlParams(params)
		if strings.Contains(url, "?") {
			url = url + "&" + paramsStr
		} else {
			url = url + "?" + paramsStr
		}
	}
	req, _ := http.NewRequest(http.MethodGet, url, nil)
	if header != nil {
		for key, value := range header {
			req.Header.Set(key, value)
		}
	}
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Errorf("%s Get请求失败：%v", url, err)
		return ""
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
		}
	}(resp.Body)
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Errorf("%s 读取Body失败：%v", url, err)
		return ""
	}
	return string(body)
}

func PostJson(url string, json string) string {
	var data map[string]any
	jsonTool.Unmarshal([]byte(json), &data)
	return PostJsonD(url, data)
}

func PostJsonH(url string, json string, header map[string]string) string {
	var data map[string]any
	jsonTool.Unmarshal([]byte(json), &data)
	return PostJsonDH(url, data, header)
}

func PostJsonD(url string, data map[string]any) string {
	return Post(url, true, data, nil)
}

func PostJsonDH(url string, data map[string]any, header map[string]string) string {
	return Post(url, true, data, header)
}

func PostForm(url string, data map[string]any) string {
	return Post(url, false, data, nil)
}

func PostFormH(url string, data map[string]any, header map[string]string) string {
	return Post(url, false, data, header)
}

func Post(url string, useJson bool, data map[string]any, header map[string]string) string {
	var req *http.Request
	var err error
	var reqBody []byte
	var rspBody []byte

	if useJson {
		reqBody, _ = jsonTool.Marshal(data)
		req, _ = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(reqBody))
		req.Header.Set("accept", "application/json")
		req.Header.Set("content-type", "application/json")
	} else {
		req, _ = http.NewRequest(http.MethodPost, url, strings.NewReader(UrlParams(data)))
		req.Header.Set("content-type", "application/x-www-form-urlencoded")
	}
	if header != nil {
		for key, value := range header {
			req.Header.Set(key, value)
		}
	}

	client := &http.Client{}
	var resp *http.Response
	resp, err = client.Do(req)
	if err != nil {
		log.Errorf("%s 请求失败：%v", url, err)
		return ""
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
		}
	}(resp.Body)
	rspBody, err = io.ReadAll(resp.Body)
	if err != nil {
		log.Errorf("%s 读取Body失败：%v", url, err)
		return ""
	}
	return string(rspBody)
}

func UrlParams(data map[string]any) string {
	formData := urlTool.Values{}
	for key, value := range data {
		formData.Add(key, fmt.Sprintf("%v", value))
	}
	return formData.Encode()
}
